#!/usr/bin/env bash

source ./config/env.sh
clear

mkdir -p ${K8S_CA_DIR}/etcd

cat > ${K8S_CA_DIR}/etcd/etcd-csr.json <<EOF
{
  "CN": "etcd",
  "hosts": [
    "127.0.0.1",
  ],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "k8s",
      "OU": "4Paradigm"
    }
  ]
}
EOF

sed -i '5 i\ \ \ \ "'`echo ${MASTER_IP} |  cut -d " " -f 1`'"' ${K8S_CA_DIR}/etcd/etcd-csr.json

for ((i=2; $i<=${MASTER};i++))
do
    sed -i '5 i\ \ \ \ "'`echo ${MASTER_IP} |  cut -d " " -f $i`'",' ${K8S_CA_DIR}/etcd/etcd-csr.json
done



cd ${K8S_CA_DIR}/etcd

${K8S_BIN_DIR}/cfssl gencert -ca=${K8S_CA_DIR}/admin/ca.pem \
    -ca-key=${K8S_CA_DIR}/admin/ca-key.pem \
    -config=${K8S_CA_DIR}/admin/ca-config.json \
    -profile=kubernetes etcd-csr.json | cfssljson -bare etcd


for master_ip in ${MASTER_IP}
do
    echo "=========================================== ${master_ip} ==========================================="
    ssh root@${master_ip} "test -e ${K8S_CA_DIR}/etcd || mkdir -p ${K8S_CA_DIR}/etcd"
    scp ${K8S_CA_DIR}/etcd/etcd*.pem root@${master_ip}:${K8S_CA_DIR}/etcd
done

mkdir -p ${K8S_DATA}/config/etcd

cat > ${K8S_DATA}/config/etcd/etcd.service.template <<EOF
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
Documentation=https://github.com/coreos

[Service]
Type=notify
WorkingDirectory=${K8S_DATA}/data/etcd/data
ExecStart=${K8S_BIN_DIR}/etcd \\
  --data-dir=${K8S_DATA}/data/etcd/data \\
  --wal-dir=${K8S_DATA}/data/etcd/wal \\
  --name=##NODE_NAME## \\
  --cert-file=${K8S_CA_DIR}/etcd/etcd.pem \\
  --key-file=${K8S_CA_DIR}/etcd/etcd-key.pem \\
  --trusted-ca-file=${K8S_CA_DIR}/admin/ca.pem \\
  --peer-cert-file=${K8S_CA_DIR}/etcd/etcd.pem \\
  --peer-key-file=${K8S_CA_DIR}/etcd/etcd-key.pem \\
  --peer-trusted-ca-file=${K8S_CA_DIR}/admin/ca.pem \\
  --peer-client-cert-auth \\
  --client-cert-auth \\
  --listen-peer-urls=https://##NODE_IP##:2380 \\
  --initial-advertise-peer-urls=https://##NODE_IP##:2380 \\
  --listen-client-urls=https://##NODE_IP##:2379,http://127.0.0.1:2379 \\
  --advertise-client-urls=https://##NODE_IP##:2379 \\
  --initial-cluster-token=etcd-cluster-0 \\
  --initial-cluster=${ETCD_ENDPOINT} \\
  --initial-cluster-state=new \\
  --auto-compaction-mode=periodic \\
  --auto-compaction-retention=1 \\
  --max-request-bytes=33554432 \\
  --quota-backend-bytes=6442450944 \\
  --heartbeat-interval=250 \\
  --election-timeout=2000
Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

for ((i=1;i<=${MASTER};i++))
do
    sed -e "s/##NODE_NAME##/`echo ${MASTER_HOSTNAME} | cut -d " " -f $i`/" -e "s/##NODE_IP##/`echo ${MASTER_IP} | cut -d " " -f $i`/" ${K8S_DATA}/config/etcd/etcd.service.template > ${K8S_DATA}/config/etcd/etcd-`echo ${MASTER_IP} | cut -d " " -f $i`.service
done


for master_ip in ${MASTER_IP}
do
    echo "=========================================== ${master_ip} ==========================================="
    scp ${K8S_DATA}/config/etcd/etcd-${master_ip}.service root@${master_ip}:/etc/systemd/system/etcd.service
    ssh root@${master_ip} "mkdir -p ${K8S_DATA}/data/etcd/{data,wal}"
    ssh root@${master_ip} "systemctl daemon-reload && systemctl enable etcd && systemctl restart etcd &"
done


for master_ip in ${MASTER_IP}
do
    sleep 10
    ssh root@${master_ip} "sudo systemctl restart etcd"
done


for master_ip in ${MASTER_IP}
do
  echo "=========================================== ${master_ip} ==========================================="
  ssh root@${master_ip} "sudo systemctl status etcd | grep Active"
done


sleep 10
for master_ip in ${MASTER_IP}
do
    echo "=========================================== ${master_ip} ==========================================="
    ETCDCTL_API=3 ${K8S_BIN_DIR}/etcdctl \
    --endpoints=https://${master_ip}:2379 \
    --cacert=${K8S_CA_DIR}/admin/ca.pem \
    --cert=${K8S_CA_DIR}/etcd/etcd.pem \
    --key=${K8S_CA_DIR}/etcd/etcd-key.pem endpoint health
done


ETCDCTL_API=3 ${K8S_BIN_DIR}/etcdctl \
  -w table --cacert=${K8S_CA_DIR}/admin/ca.pem \
  --cert=${K8S_CA_DIR}/etcd/etcd.pem \
  --key=${K8S_CA_DIR}/etcd/etcd-key.pem \
  --endpoints=${ETCD_NODES} endpoint status 


mkdir -p ${K8S_LOGS_DIR}

if [ $? == 0 ]
then
  echo "etcd install succeed"
else
  echo -e "\033[31m etcd install failed please chekc ${K8S_LOGS_DIR}/etcd-instal.log \033[0m" 
  exit
fi
